package com.example.generator.code;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;

import java.util.Collections;
import java.util.Date;

public class CodeGenerator {
    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/learning?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC", "root", "root")
                .globalConfig(builder -> {
                    builder.author("tsj \n DatasourceInstance \n "+ new Date()) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("D:\\code\\g"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.zkkz.boot") // 设置父包名
                            .moduleName("dhua") // 设置父包模块名
                            .mapper("mapper")
                            .service("service")
                            .controller("controller")
                            .entity("pojo")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "D:\\code\\g")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("user") // 设置需要生成的表名
                            .addTablePrefix("dhua","campus", "o_", "elderly_","hik_"); // 设置过滤表前缀
                }).strategyConfig(builder -> {
                    builder.enableCapitalMode()
                            .entityBuilder()
                            .enableLombok()
                            //.superClass("com.zkkz.gb.pojo.BaseEntity")
                            .disableSerialVersionUID()
                            .enableTableFieldAnnotation()
                            .addTableFills(new Column("create_time", FieldFill.DEFAULT))
                            .addTableFills(new Property("updateTime", FieldFill.DEFAULT))
                            .idType(IdType.AUTO);
                }).strategyConfig(builder -> {
                    builder.enableCapitalMode()
                            .mapperBuilder()
                            // 设置父类
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
